import glob
import numpy as np
import pandas as pd
from pyecharts.charts import Bar, Scatter, Pie, WordCloud
from pyecharts.components import Table
from pyecharts import options as opts
from pyecharts.globals import CurrentConfig, NotebookType, SymbolType, ThemeType


CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB

# 导入输出图片工具
from pyecharts.render import make_snapshot
# 使用snapshot-selenium 渲染图片
from snapshot_selenium import snapshot

import os

if not os.path.exists("img"):
    os.mkdir("img")

# 各个类别的App的数量
category_count = pd.read_csv(glob.glob('results/category_count.csv/*.csv')[0])
bar_category_count = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
        .add_xaxis(category_count['Category'].tolist()[:10])
        .add_yaxis("", category_count['count'].tolist()[:10])
        .set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
                         title_opts={'text': 'App数量最多的10个类别'})
)
# bar_category_count.render_notebook()
make_snapshot(snapshot, bar_category_count.render(), "img/category_count.png")

# App评分分布
rating_dist = pd.read_csv(glob.glob('results/rating_distrib.csv/*.csv')[0])
pie_rating_dist = (
    Pie(init_opts=opts.InitOpts(theme=ThemeType.INFOGRAPHIC))
        .add("", [list(z) for z in zip(rating_dist['Rating'].tolist(), rating_dist['count'].tolist())])
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{d}%"))
        .set_global_opts(
        title_opts=opts.TitleOpts(title="用户评分分布"),
        legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical")
    )
)

# pie_rating_dist.render_notebook()
make_snapshot(snapshot, pie_rating_dist.render(), "img/rating_dist.png")

# App评论数分布
review_dist = pd.read_csv(glob.glob('results/reviews_distrib.csv/*.csv')[0])
pie_review_dist = (
    Pie(init_opts=opts.InitOpts(theme=ThemeType.INFOGRAPHIC))
        .add("", [list(z) for z in zip(review_dist['Reviews'].tolist(), review_dist['count'].tolist())])
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{d}"))
        .set_global_opts(
        title_opts=opts.TitleOpts(title="评论数量分布"),
        legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical")
    )
)

# pie_review_dist.render_notebook()
make_snapshot(snapshot, pie_review_dist.render(), "img/review_dist.png")

# App安装量分布
install_dist = pd.read_csv(glob.glob('results/installs_distrib.csv/*.csv')[0])
pie_install_dist = (
    Pie(init_opts=opts.InitOpts(theme=ThemeType.INFOGRAPHIC))
        .add("", [list(z) for z in zip(install_dist['Installs'].tolist(), install_dist['count'].tolist())])
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{d}"))
        .set_global_opts(
        title_opts=opts.TitleOpts(title="安装次数分布"),
        legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical")
    )
)

# pie_install_dist.render_notebook()
make_snapshot(snapshot, pie_install_dist.render(), "img/install_dist.png")

# 安装量超过1亿的App
top_install = pd.read_csv(glob.glob('results/top_installs.csv/*.csv')[0])
top_install_list = list(top_install.itertuples(index=False, name=None))
wc_top_install = WordCloud()
wc_top_install.add('', top_install_list, shape=SymbolType.DIAMOND, word_size_range=[10, 25])
# wc_top_install.render_notebook()
make_snapshot(snapshot, wc_top_install.render(), "img/top_install.png")

# 各类别中安装量前5的App
top_5_install_each_category = pd.read_csv(glob.glob('results/top_5_install_each_category.csv/*.csv')[0])
top_5_install_each_category_list = list(top_5_install_each_category.itertuples(index=False, name=None))
wc_top_5_install = WordCloud()
wc_top_5_install.add('', top_5_install_each_category_list, shape=SymbolType.DIAMOND, word_size_range=[10, 25])
wc_top_5_install.render_notebook()
make_snapshot(snapshot, wc_top_5_install.render(), 'img/top_5_each_category.png')

# 免费App与付费App评分、评论数、安装量对比
free_vs_paid = pd.read_csv(glob.glob('results/free_vs_paid.csv/*.csv')[0])
table_free_vs_paid = Table()
headers = list(free_vs_paid.columns)
rows = [list(free_vs_paid.loc[index]) for index in free_vs_paid.index]
table_free_vs_paid.add(headers, rows)
# table_free_vs_paid.add(free_vs_paid.columns.tolist(), free_vs_paid.values)
table_free_vs_paid.set_global_opts(
    title_opts=opts.ComponentTitleOpts(title="免费应用与付费应用对比")
)
table_free_vs_paid.render_notebook()

# 付费App价格与评论数和安装量的关系

price_reviews_installs = pd.read_csv(glob.glob('results/price_reviews_installs.csv/*.csv')[0])

bar_price_reviews_installs = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))
        .add_xaxis(price_reviews_installs['Price'].tolist())
        .add_yaxis("Reviews", np.sqrt(price_reviews_installs['Reviews']).tolist())
        .add_yaxis("Installs", np.sqrt(price_reviews_installs['Installs']).tolist())
        .set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-90)),
                         title_opts={'text': '价格与评论数和安装量之间的关系'})
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)

# bar_price_reviews_installs.render_notebook()
make_snapshot(snapshot, bar_price_reviews_installs.render(), 'img/price_reviews_installs.png')

# App的评论数与安装量之间的关系
reviews_installs = pd.read_csv(glob.glob('results/reviews_installs.csv/*.csv')[0])

bar_reviews_installs = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))
        .add_xaxis(np.log(reviews_installs['Installs']).tolist())
        .add_yaxis("", np.log(reviews_installs['Reviews']).tolist())
        .set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-90)),
                         title_opts={'text': '安装量和评论数之间的关系'})
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)

# bar_reviews_installs.render_notebook()
make_snapshot(snapshot, bar_reviews_installs.render(), 'img/reviews_installs.png')

# 用户评分与评论数和安装量之间的关系
rating_reviews_installs = pd.read_csv(glob.glob('results/rating_reviews_installs.csv/*.csv')[0])

bar_rating_reviews_installs = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))
        .add_xaxis(rating_reviews_installs['Rating'].tolist())
        .add_yaxis("Reviews", np.log(rating_reviews_installs['Reviews']).tolist())
        .add_yaxis("Installs", np.log(rating_reviews_installs['Installs']).tolist())
        .set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-90)),
                         title_opts={'text': '用户评分与安装量和评论数之间的关系'})
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
)

# bar_rating_reviews_installs.render_notebook()

make_snapshot(snapshot, bar_rating_reviews_installs.render(), 'img/rating_reviews_installs.png')